package com.wxtf.platform.plugins.user;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.wxtf.platform.interfaces.CommonsCallProvider;
import com.wxtf.platform.supers.LogicBase;
import com.wxtf.platform.tools.AccessDataBaseUtil;
import com.wxtf.platform.tools.ConstantInfoHelper;
import com.wxtf.platform.tools.FormatUtil;
import com.wxtf.platform.tools.MD5;

/**
 * <li>Title: UserAction.java</li>
 * <li>Project: baseplatform</li>
 * <li>Package: com.wxtf.platform.plugins.user</li>
 * <li>Description: 用户管理-表T_USER</li>
 * <li>Copyright: Copyright (c) 2012</li>
 * <li>Company: wxthtf Technologies </li>
 * <li>Created on Jul 5, 2012 2:53:14 PM</li>
 * 
 * @author chun_chang
 * @version 1.0.0.0
 * 
 */
public class UserAction extends LogicBase implements CommonsCallProvider{
	private static Log log = LogFactory.getLog(UserAction.class);

	public UserAction(String jsonArray){

	}

	@Override
	public Object onInit(Object jsonArray) {
		return null;
	}

	@Override
	public Object onCreate(Object jsonArray) {
		return null;
	}

	@Override
	public Object onDestory(Object jsonArray) {
		return null;
	}

	@Override
	public Object insert(Object jsonArray) throws Exception {
		JSONArray array = JSONArray.fromObject(jsonArray);
		JSONObject jsonObject = JSONObject.fromObject(array.getString(0));
		
		String loginSql = "select count(*) from t_user where action = ? ";
		List<Object> ls = AccessDataBaseUtil.query4List(loginSql, new Object[] { jsonObject.getString("action") });
		int hasId = Integer.parseInt(ls.get(0).toString());
		if(hasId > 0){
			return "\"登录账号已经存在\"";
		}
		
		String[] fields = { "user_id", "uparent_id", "caption", "logic_code", "action" };
		String[] fieldsInfo = { "user_id", "user_status", "real_name", "user_email", "user_passwd", "user_office_tel",
				"user_tel", "user_fax", "user_address", "user_created", "user_modified", "user_memo1",
				"user_mobilephone", "user_postal", "user_sex", "user_birthday", "user_education", "user_position_id" };
		
		Object[] obj = new Object[fields.length];
		Object[] objInfo = new Object[fieldsInfo.length];
		for(int j = 0; j < fields.length; j++) {
			obj[j] = jsonObject.getString(fields[j]);
		}

		String selectCols = "";
		for(int j = 0; j < fieldsInfo.length; j++) {
			objInfo[j] = jsonObject.getString(fieldsInfo[j]);
			if(j != fieldsInfo.length - 1) {
				selectCols += fieldsInfo[j] + ",";
			} else {
				selectCols += fieldsInfo[j];
			}
		}

		obj[0] = AccessDataBaseUtil.getPKUUID();
		// 重置用户的ID
		objInfo[0] = obj[0];
		// 重置用户的密码
		objInfo[4] = AccessDataBaseUtil.getKey();

		
		List<String> sqlList = new ArrayList<String>();
		StringBuffer buff = new StringBuffer();

		buff.append("insert into T_USER(USER_ID,uparent_id,caption,logic_code,action) values('")
			.append(obj[0]).append("','").append(obj[1]).append("',")
			.append("'").append(obj[2]).append("',")
			.append("'").append(obj[3]).append("',")
			.append("'").append(obj[4]).append("')");
		sqlList.add(buff.toString());
		buff.setLength(0);
		
		buff.append("insert into T_USER_INFO(").append(selectCols).append(") values(");
		for(int i = 0; i < objInfo.length; i++) {
			buff.append("'").append(objInfo[i]).append("',");
		}
		buff.deleteCharAt(buff.length() - 1).append(")");
		sqlList.add(buff.toString());
		AccessDataBaseUtil.executeSQLUpdate(sqlList);
		return "\"新建成功\"";
	}

	/**
	 * 描述：批量新建
	 */
	public Object totalInsert(Object jsonArray) throws Exception {
		List<String> list = new ArrayList<String>();
		JSONArray array = JSONArray.fromObject(jsonArray);
		JSONObject jsonObject = JSONObject.fromObject(array.getString(0));
		int total = Integer.parseInt(jsonObject.getString("total"));
		String preFix = jsonObject.getString("preFix");
		for(int i = 0; i < total; i++) {
			String timeName = FormatUtil.getCurentDateTime4ms() + i;
			String username = preFix + (timeName.substring(timeName.length() - 8));
			String id = AccessDataBaseUtil.getPKUUID();
			list.add("insert into T_USER(USER_ID,caption,action) values('" + id + "','" + username + "','" + username + "')");
			list.add("insert into T_USER_INFO(USER_ID,user_passwd,real_name,user_education,user_modified) values('" + id + "','" + AccessDataBaseUtil.getKey() + "','Aqua','index.jsp','"+FormatUtil.getCurentDateTime()+"')");
		}
		AccessDataBaseUtil.executeSQLUpdate(list);
		return null;
	}

	/**
	 * 描述：密码重置
	 */
	public Object passwordReset(Object jsonArray) throws Exception {
		JSONArray array = JSONArray.fromObject(jsonArray);
		JSONObject jsonObject = JSONObject.fromObject(array.getString(0));
		String id = jsonObject.getString("ids");
		String[] ids = id.split(",");
		String sql = "update T_USER_info set user_passwd='" + AccessDataBaseUtil.getKey() + "' where USER_ID = ?";
		AccessDataBaseUtil.deleteByIds(sql, ids);

		return null;
	}

	/**
	 * 描述：不允许登录
	 */
	public Object userAcessNotLogin(Object jsonArray) throws Exception {
		JSONArray array = JSONArray.fromObject(jsonArray);
		JSONObject jsonObject = JSONObject.fromObject(array.getString(0));
		String id = jsonObject.getString("ids");
		String[] sql = { "update t_user_info set user_status='0' where user_id  in (" + id + ")" };
		AccessDataBaseUtil.executeSQLUpdate(sql);
		return null;
	}

	/**
	 * 描述：允许登录
	 */
	public Object userAcessLogin(Object jsonArray) throws Exception {
		JSONArray array = JSONArray.fromObject(jsonArray);
		JSONObject jsonObject = JSONObject.fromObject(array.getString(0));
		String id = jsonObject.getString("ids");
		id = id.replaceAll(",", "','");
		String[] sql = { "update t_user_info set user_status='1' where user_id  in ('" + id + "')" };
		AccessDataBaseUtil.executeSQLUpdate(sql);
		return null;
	}

	/**
	 * 描述：更新用户方法
	 */
	@Override
	public Object update(Object jsonArray) throws Exception {
		List<String> list = new ArrayList<String>();
		JSONArray array = JSONArray.fromObject(jsonArray);
		JSONObject jsonObject = JSONObject.fromObject(array.getString(0));
		
		String loginSql = "select count(*) from t_user where action = ? and USER_ID <> ?";
		List<Object> ls = AccessDataBaseUtil.query4List(loginSql, new Object[] { jsonObject.getString("action"),jsonObject.getString("user_id") });
		int hasId = Integer.parseInt(ls.get(0).toString());
		if(hasId > 0){
			return "\"登录账号已经存在\"";
		}
		
		StringBuffer buff = new StringBuffer();
		buff.append("update T_USER set ")
			.append("uparent_id = ").append(jsonObject.getString("uparent_id")).append(",")
			.append("caption = '").append(jsonObject.getString("caption")).append("',")
			.append("logic_code = '").append(jsonObject.getString("logic_code")).append("',")
			.append("action = '").append(jsonObject.getString("action")).append("'")
			.append(" where user_id = '").append(jsonObject.getString("user_id")).append("'");
		list.add(buff.toString());
		buff.setLength(0);
		
		buff.append("update T_USER_INFO set ")
			.append("user_email = '").append(jsonObject.getString("user_email")).append("',")
			.append("user_office_tel = '").append(jsonObject.getString("user_office_tel")).append("',")
			.append("user_tel = '").append(jsonObject.getString("user_tel")).append("',")
			.append("user_fax = '").append(jsonObject.getString("user_fax")).append("',")
			.append("user_address = '").append(jsonObject.getString("user_address")).append("',")
			.append("user_created = '").append(jsonObject.getString("user_created")).append("',")
			.append("user_modified = '").append(jsonObject.getString("user_modified")).append("',")
			.append("user_memo1 = '").append(jsonObject.getString("user_memo1")).append("',")
			.append("user_mobilephone = '").append(jsonObject.getString("user_mobilephone")).append("',")
			.append("user_postal = '").append(jsonObject.getString("user_postal")).append("',")
			.append("user_sex = '").append(jsonObject.getString("user_sex")).append("',")
			.append("user_birthday = '").append(jsonObject.getString("user_birthday")).append("',")
			.append("user_position_id = '").append(jsonObject.getString("user_position_id")).append("'")
			.append(" where user_id = '").append(jsonObject.getString("user_id")).append("'");
		list.add(buff.toString());
		
		AccessDataBaseUtil.executeSQLUpdate(list);
		return "\"修改成功\"";
	}

	public Object update1(Object jsonArray) throws Exception {
		List<Object[]> list = new ArrayList<Object[]>();
		String[] fields = { "user_email", "user_office_tel", "user_tel",
				"user_fax", "user_address", "user_created", "user_modified", "user_memo1", "user_mobilephone",
				"user_postal", "user_sex", "user_birthday", "user_position_id", "user_id" };

		JSONArray array = JSONArray.fromObject(jsonArray);
		JSONObject jsonObject = JSONObject.fromObject(array.getString(0));
		Object[] obj = new Object[fields.length];
		for(int j = 0; j < fields.length; j++) {
			obj[j] = jsonObject.getString(fields[j]);
		}
		list.add(obj);

		sql = "update T_USER_INFO set user_email = ?,user_office_tel = ?,user_tel = ?,user_fax = ?,user_address = ?,user_created = ?,user_modified = ?,user_memo1 = ?,user_mobilephone = ?,user_postal = ?,user_sex = ?,user_birthday = ?,user_position_id = ? where USER_ID = ?";
		AccessDataBaseUtil.executeSQLUpdate(sql, list);
		return "\"修改成功\"";
	}

	@Override
	public Object delete(Object jsonArray) throws Exception {
		JSONArray array = JSONArray.fromObject(jsonArray);
		JSONObject jsonObject = JSONObject.fromObject(array.getString(0));
		String id = jsonObject.getString("ids");
		String[] ids = id.split(",");
		String sql = "delete from T_USER where USER_ID = ?";
		AccessDataBaseUtil.deleteByIds(sql, ids);

		return "\"删除成功\"";
	}

	@Override
	public Object updateBatch(Object jsonArray) throws Exception {
		return null;
	}

	@Override
	public Object select(Object jsonArray) throws Exception {
		UserBean user = (UserBean)this.getRequest().getSession().getAttribute("userBean");
		String orgId = user.getCurOrgId();
		if(StringUtils.isBlank(orgId)) {
			return "{\"Total\":\"0\",\"Rows\":[]}";
		}
		String userName = "";
		String loginName = "";
		String sWhere = "";// 高级查询记录条件内容

		if(!jsonArray.equals("[]")) {
			JSONArray array = JSONArray.fromObject(jsonArray);
			JSONObject jsonObject = JSONObject.fromObject(array.getString(0));
			if(jsonObject.containsKey("search_userName")) {
				userName = jsonObject.getString("search_userName");
			}

			if(jsonObject.containsKey("search_loginName")) {
				loginName = jsonObject.getString("search_loginName");
			}
			if(jsonObject.containsKey("sWhere")) {
				sWhere = jsonObject.getString("sWhere");
				if(sWhere == null || sWhere.equals("null")) {
					sWhere = "";
				} else {
					// 为表明替换别名，使其和sql语句中对应
					sWhere = sWhere.replace("t_user_info", "s");
					sWhere = sWhere.replace("t_user", "r");
				}
			}
		}
		String subOrg = user.getCurSubOrgId();
		String curOrg = user.getCurOrgId();
		subOrg = subOrg.replaceAll(curOrg, "");
		String sql = "";
		if("0".equals(user.getUserId())){//sa查看所有人
			sql = " from t_user r, t_user_info s where r.user_id = s.user_id";
		} else {//看下级机构的人
			sql = " from t_user_info s,t_user r where s.user_id = r.user_id and r.user_id <> 0 and r.user_id <> '" + user.getUserId() + "' and r.user_id in( select t.user_id from t_user_department t where t.department_id in (" + subOrg + "))";
		}
		if(StringUtils.isNotBlank(loginName)){
			sql += " and r.action like '%" + loginName + "%' ";
		}
		if(StringUtils.isNotBlank(userName)){
			sql += " and r.caption like '%" + userName + "%'";
		}
		sql += sWhere;
		
		String countSql = "select count(*)" + sql;
		String querySql = "select s.user_id,s.user_status,s.real_name,s.user_email,s.user_passwd,s.user_office_tel,s.user_tel,s.user_fax,s.user_address,s.user_created,s.user_modified,s.user_memo1,s.user_mobilephone,s.user_postal,s.user_sex,s.user_birthday,s.user_education,s.user_position_id,r.uparent_id,r.caption,r.logic_code,r.action" + sql;
		
		String sortname = this.request.getParameter("sortname");
		String sortorder = this.request.getParameter("sortorder");
		if(StringUtils.isBlank(sortname)) {
			querySql += " order by s.user_modified desc";
		} else {
			querySql += " order by r." + sortname + " " + sortorder;
		}
		String[] fields = {
				"user_id", "user_status", "real_name", "user_email", "user_passwd", "user_office_tel",
				"user_tel", "user_fax", "user_address", "user_created", "user_modified", "user_memo1",
				"user_mobilephone", "user_postal", "user_sex", "user_birthday", "user_education", "user_position_id",
				"uparent_id", "caption", "logic_code", "action"
		};
		Object[] params = {};
		return AccessDataBaseUtil.query4SQLPagging(fields, countSql, querySql, this.getPage(), this.getPagesize(), params);
	}

	@Override
	public Object recordCount(Object jsonArray) {
		return null;
	}

	@Override
	public Object setRequest(HttpServletRequest request) {
		this.request = request;
		return this.request;
	}

	public Object saveMenu(Object jsonArray) throws Exception {
		JSONArray array = JSONArray.fromObject(jsonArray);
		JSONObject jsonObject = JSONObject.fromObject(array.getString(0));
		String id = jsonObject.getString("userId");
		String permission_id = jsonObject.getString("permissionId");
		String[] ids = permission_id.split(",");
		List<String> list = new ArrayList<String>();
		StringBuffer buff = new StringBuffer();

		buff.append("delete from t_user_permission where user_id = '").append(id).append("'");
		list.add(buff.toString());
		
		if(!"".equals(permission_id)){
			buff.setLength(0);
			for(String id1: ids) {
				buff.append("insert into T_USER_PERMISSION(U_P_ID,USER_ID,PERMISSION_ID)").append("values(").append("'")
				.append(AccessDataBaseUtil.getPKUUID()).append("','").append(id).append("','").append(id1).append("')");
				list.add(buff.toString());
				buff.setLength(0);
			}
		}
		AccessDataBaseUtil.executeSQLUpdate(list);
		return "\"操作成功\"";
	}

	public Object saveRole(Object jsonArray) throws Exception {
		JSONArray array = JSONArray.fromObject(jsonArray);
		JSONObject jsonObject = JSONObject.fromObject(array.getString(0));
		String id = jsonObject.getString("user_id");
		String role_id = jsonObject.getString("role_id");
		String[] ids = role_id.split(",");

		List<String> list = new ArrayList<String>();
		StringBuffer buff = new StringBuffer();

		for(String id1: ids) {
			buff.append("'").append(id1).append("',");
		}
		buff.append("''");
		sql = "delete from T_USER_ROLE where USER_ID = '" + id + "' and ROLE_ID in (" + buff.toString() + ")";
		list.add(sql);

		buff.setLength(0);
		for(String id1: ids) {
			buff.append("insert into T_USER_ROLE(U_R_ID,USER_ID,ROLE_ID)").append("values(")
				.append("'").append(AccessDataBaseUtil.getPKUUID()).append("','")
				.append(id).append("','").append(id1).append("')");
			list.add(buff.toString());
			buff.setLength(0);
		}
		AccessDataBaseUtil.executeSQLUpdate(list);
		return null;
	}

	public Object delRole(Object jsonArray) throws Exception {
		JSONArray array = JSONArray.fromObject(jsonArray);
		JSONObject jsonObject = JSONObject.fromObject(array.getString(0));
		String id = jsonObject.getString("user_id");
		String role_id = jsonObject.getString("role_id");
		String[] ids = role_id.split(",");

		StringBuffer buff = new StringBuffer();
		for(String id1: ids) {
			buff.append("'").append(id1).append("',");
		}
		buff.append("''");

		sql = "delete from T_USER_ROLE where USER_ID = ? and ROLE_ID in (" + buff.toString() + ")";
		AccessDataBaseUtil.executeSQLUpdate(sql, new Object[] { id });

		return null;
	}

	public Object saveGroup(Object jsonArray) throws Exception {
		JSONArray array = JSONArray.fromObject(jsonArray);
		JSONObject jsonObject = JSONObject.fromObject(array.getString(0));
		String id = jsonObject.getString("user_id");
		String group_id = jsonObject.getString("group_id");
		String[] ids = group_id.split(",");

		List<String> list = new ArrayList<String>();
		StringBuffer buff = new StringBuffer();
		buff.append("''");
		for(int i = 0; i < ids.length; i++) {
			buff.append(",'").append(ids[i]).append("'");
		}

		buff.setLength(0);
		buff.append("delete from t_user_group where USER_ID = '").append(id).append("'");
		list.add(buff.toString());
		if(!"".equals(group_id)){
			buff.setLength(0);
			for(int i = 0; i < ids.length; i++) {
				buff.append("insert into t_user_group(U_G_ID,USER_ID,GROUP_ID) values('")
					.append(AccessDataBaseUtil.getPKUUID()).append("','")
					.append(id).append("','")
					.append(ids[i]).append("')");
				list.add(buff.toString());
				buff.setLength(0);
			}
		}

		AccessDataBaseUtil.executeSQLUpdate(list);
		return null;
	}

	public Object listRole(Object jsonArray) throws Exception {
		JSONArray array = JSONArray.fromObject(jsonArray);
		JSONObject jsonObject = JSONObject.fromObject(array.getString(0));
		String id = jsonObject.getString("user_id");
		String name = jsonObject.getString("name");

		sql = " from t_role r left join t_user_role s on r.role_id = s.role_id and s.user_id = ? where 1=1 AND r.role_id NOT IN (SELECT role_id FROM t_user_role WHERE user_id = ?)";
		if(StringUtils.isNotBlank(name)) {
			sql += " and r.caption like '%" + name + "%'";
		}

		String countSql = "select count(*)" + sql;
		String querySql = "select r.role_id, r.caption, s.user_id " + sql + " order by LOGIC_CODE asc";
		String[] fields = { "role_id", "caption", "user_id" };
		Object[] params = { id, id };
		return AccessDataBaseUtil.query4SQLPagging(fields, countSql, querySql, this.getPage(), this.getPagesize(), params);
	}

	/**
	 * 描述：saveOrg 用户选择机构后保存机构
	 * 
	 * @param jsonArray
	 * @return
	 * @throws Exception
	 * @CreateOn Jul 19, 2012 3:40:55 PM
	 * @author chun_chang
	 */
	public Object saveOrg(Object jsonArray) throws Exception {
		JSONArray array = JSONArray.fromObject(jsonArray);
		JSONObject jsonObject = JSONObject.fromObject(array.getString(0));
		String id = jsonObject.getString("user_id");
		String org_id = jsonObject.getString("org_id");
		String[] ids = org_id.split(",");

		List<String> list = new ArrayList<String>();
		StringBuffer buff = new StringBuffer();

		buff.append("delete from T_USER_DEPARTMENT where USER_ID = '").append(id).append("'");
		list.add(buff.toString());
		
		if(!"".equals(org_id)){
			buff.setLength(0);
			for(String id1: ids) {
				buff.append("insert into T_USER_DEPARTMENT(U_D_ID,USER_ID,DEPARTMENT_ID)").append("values(")
					.append("'").append(AccessDataBaseUtil.getPKUUID()).append("',")
					.append("'").append(id).append("',")
					.append("'").append(id1).append("')");
				list.add(buff.toString());
				buff.setLength(0);
			}
		}
		AccessDataBaseUtil.executeSQLUpdate(list);
		return null;
	}

	public Object listRole4User(Object jsonArray) throws Exception {
		String userId = "";
		String roleName = "";

		if(!jsonArray.equals("[]")) {
			JSONArray array = JSONArray.fromObject(jsonArray);
			JSONObject jsonObject = JSONObject.fromObject(array.getString(0));
			if(jsonObject.containsKey("user_id")) {
				userId += jsonObject.getString("user_id");
			}

			if(jsonObject.containsKey("name")) {
				roleName += jsonObject.getString("name");
			}
		}

		sql = " from t_role r,t_user_role s where r.role_id = s.role_id and s.user_id = ? ";
		if(StringUtils.isNotBlank(roleName)) {
			sql += " and r.caption like '%" + roleName + "%'";
		}

		String countSql = "select count(*)" + sql;
		String querySql = "select r.role_id, r.caption" + sql + " order by r.LOGIC_CODE asc";
		String[] fields = { "role_id", "caption" };
		return AccessDataBaseUtil.query4SQLPagging(fields, countSql, querySql, this.getPage(), this.getPagesize(), new Object[] { userId });
	}

	/**
	 * 描述：根据用户userId查询一条的方法
	 */
	public Object getUserByUserId(Object jsonArray) throws Exception {
		JSONArray array = JSONArray.fromObject(jsonArray);
		JSONObject jsonObject = JSONObject.fromObject(array.getString(0));
		String userId = jsonObject.getString("userId");
		sql = "select r.user_id,r.caption,s.user_passwd,r.action,s.user_email,s.user_office_tel,s.user_mobilephone,s.user_tel,s.user_fax,s.user_address,s.user_sex,s.user_birthday,s.USER_EDUCATION,s.user_position_id from t_user r,t_user_info s where r.user_id=s.user_id and r.user_id=?";
		Object[] params = { userId };
		String[] fields = { "user_id", "caption", "user_passwd", "action", "user_email", "user_office_tel",
				"user_mobilephone", "user_tel", "user_fax", "user_address", "user_sex", "user_birthday",
				"user_eduation","user_position_id" };
		JSONArray temp = JSONArray.fromObject(AccessDataBaseUtil.query(fields, sql, params));
		return temp.toString().substring(1, temp.toString().length() - 1);
	}

	/**
	 * 描述：根据用户userId修改个人信息
	 */
	public Object modifyUser(Object jsonArray) throws Exception {
		JSONArray array = JSONArray.fromObject(jsonArray);
		JSONObject jsonObject = JSONObject.fromObject(array.getString(0));
		String[] fields = { "user_id", "caption", "action", "user_email", "user_office_tel", "user_tel",
				"user_mobilephone", "user_fax", "user_address", "user_sex", "user_birthday" };
		Object[] obj = new Object[fields.length];
		for(int j = 0; j < fields.length - 1; j++) {
			obj[j] = jsonObject.getString(fields[j + 1]);
		}
		obj[obj.length - 1] = jsonObject.getString(fields[0]);
		String useraction = jsonObject.getString("useraction");
		String action = jsonObject.getString("action");
		if(!useraction.equals(action)) {
			String loginSql = "select count(*) from t_user where action='" + jsonObject.getString("action") + "'";
			List<Object> ls = AccessDataBaseUtil.query4List(loginSql, new Object[] {});
			int hasId = Integer.parseInt(ls.get(0).toString());
			if(hasId > 0) {
				return ConstantInfoHelper.generateJSONString("\"登录名称不能重复\"");
			}
		}
		sql = "update t_user r,t_user_info s set r.caption=?,r.action=?,s.user_email=?,s.user_office_tel=?,s.user_tel=?,s.user_mobilephone=?,s.user_fax=?,s.user_address=?,s.user_sex=?,s.user_birthday=? where r.user_id=s.user_id and r.user_id=?";
		AccessDataBaseUtil.executeSQLUpdate(sql, obj);
		return "\"用户信息修改成功\"";
	}

	/**
	 * 描述：根据用户userId修改密码
	 */
	public Object modifyPwd(Object jsonArray) throws Exception {
		JSONArray array = JSONArray.fromObject(jsonArray);
		JSONObject jsonObject = JSONObject.fromObject(array.getString(0));
		String user_id = jsonObject.getString("t_user_id");
		String user_passwd = jsonObject.getString("user_passwd");
		String userPwd = jsonObject.getString("userPwd");
		String userPass = jsonObject.getString("userPass");
		String pwd = MD5.md5(userPwd);
		userPass = MD5.md5(userPass);
		if(pwd.equals(user_passwd)) {
			Object[] obj = { userPass, user_id };
			sql = "update t_user_info set user_passwd = ? where user_id = ?";
			AccessDataBaseUtil.executeSQLUpdate(sql, obj);
		} else {
			return "{\"result\":false,\"resultmessage\":\"调用失败\",\"resultrecord\":\"用户密码不正确\"}";
		}
		return "\"用户密码修改成功\"";
	}

	/**
	 * 描述：根据用户userId修改布局
	 */
	public Object modifyUserlay(Object jsonArray) throws Exception {
		UserBean us = (UserBean) ((HttpServletRequest) this.request).getSession().getAttribute("userBean");// 获得userBean信息
		JSONArray array = JSONArray.fromObject(jsonArray);
		JSONObject jsonObject = JSONObject.fromObject(array.getString(0));
		String user_id = jsonObject.getString("user_id");
		String userlay = jsonObject.getString("userlay");
		Object[] obj = { userlay, user_id };
		sql = "update t_user_info set USER_EDUCATION = ? where user_id = ?";
		AccessDataBaseUtil.executeSQLUpdate(sql, obj);
		us.setUserLay(userlay);
		return "\"用户布局修改成功\"";
	}

	// 修改皮肤
	public Object updateCss(Object jsonArray) throws Exception {
		UserBean us = (UserBean) ((HttpServletRequest) this.request).getSession().getAttribute("userBean");// 获得userBean信息

		JSONArray array = JSONArray.fromObject(jsonArray);
		JSONObject jsonObject = JSONObject.fromObject(array.getString(0));
		String userCss = jsonObject.getString("userCss");
		String sql = "update t_user_info set real_name = ? where user_id = ?";
		AccessDataBaseUtil.executeSQLUpdate(sql, new Object[] { userCss, us.getUserId() });

		us.setUserCss(userCss);
		return null;
	}
	
	public Object updViewOrg(Object jsonArray) throws Exception {
		UserBean us = (UserBean) this.request.getSession().getAttribute("userBean");// 获得userBean信息
		
		JSONArray array = JSONArray.fromObject(jsonArray);
		JSONObject jsonObject = JSONObject.fromObject(array.getString(0));
		String user_position_id = jsonObject.getString("user_position_id");
		String sql = "update t_user_info set user_position_id = ? where user_id = ?";
		AccessDataBaseUtil.executeSQLUpdate(sql, new Object[] { user_position_id, us.getUserId() });
		return "\"修改成功\"";
	}

	@Override
	public Object run() {
		Object msg = "";
		try {
			if("queryPage".equals(this.getMethodName())) {// 分页列表
				msg = this.select(this.getExtendData());
				return msg;
			} else if("save".equals(this.getMethodName())) {// 添加
				msg = this.insert(this.getLogicData());
			} else if("updateUser".equals(this.getMethodName())) {// 修改用户
				msg = this.update(this.getLogicData());
			} else if("userAcessNotLogin".equals(this.getMethodName())) {// 授权
				msg = this.userAcessNotLogin(this.getLogicData());
			} else if("userAcessLogin".equals(this.getMethodName())) {// 授权
				msg = this.userAcessLogin(this.getLogicData());
			} else if("delete".equals(this.getMethodName())) {// 删除
				msg = this.delete(this.getLogicData());
			} else if("saveOrg".equals(this.getMethodName())) {// 保存人与机构的关联关系
				msg = this.saveOrg(this.getLogicData());
			} else if("saveMenu".equals(this.getMethodName())) {// 人与菜单
				msg = this.saveMenu(this.getLogicData());
			} else if("listRole".equals(this.getMethodName())) {// 人与角色
				msg = this.listRole(this.getLogicData());
				return msg;
			} else if("saveRole".equals(this.getMethodName())) {// 人与角色
				msg = this.saveRole(this.getLogicData());
			} else if("delRole".equals(this.getMethodName())) {// 人与角色
				msg = this.delRole(this.getLogicData());
			} else if("saveGroup".equals(this.getMethodName())) {// 人与用户组
				msg = this.saveGroup(this.getLogicData());
			} else if("listRole4User".equals(this.getMethodName())) {// 根据人找角色
				msg = this.listRole4User(this.getLogicData());
				return msg;
			} else if("totalInsert".equals(this.getMethodName())) {// 批量插入
				msg = this.totalInsert(this.getLogicData());
			} else if("passwordReset".equals(this.getMethodName())) {// 密码重置
				msg = this.passwordReset(this.getLogicData());
			} else if("getUserByUserId".equals(this.getMethodName())) {// 获得用户
				msg = this.getUserByUserId(this.getLogicData());
				return msg;
			} else if("modifyUser".equals(this.getMethodName())) {
				msg = this.modifyUser(this.getLogicData());
			} else if("updateCss".equals(this.getMethodName())) {// 修改用户皮肤
				msg = this.updateCss(this.getLogicData());
			} else if("modifyPwd".equals(this.getMethodName())) {// 修改密码
				msg = this.modifyPwd(this.getLogicData());
			} else if("modifyUserlay".equals(this.getMethodName())) {// 切换布局
				msg = this.modifyUserlay(this.getLogicData());
			} else if("updViewOrg".equals(this.getMethodName())) {// 切换布局
				msg = this.updViewOrg(this.getLogicData());
			} else {
				msg = "\"没有【" + this.getMethodName() + "】该方法\"";
			}
			if(msg == null || "[null]".equals(msg)) {
				msg = "\"\"";
			}
			if(msg.toString().startsWith("{") || msg.toString().startsWith("[")){
				return msg;
			}
			msg = ConstantInfoHelper.generateJSONString(msg.toString());
		} catch(Exception e) {
			msg = "操作失败，请联系管理员";
			msg = ConstantInfoHelper.generateJSONString(ConstantInfoHelper.JSON_HEAD, ConstantInfoHelper
					.getFailResultDetail(msg.toString()), false);
			log.error(e, e);
		} finally {
			this.clear();
		}
		return msg.toString();
	}

}
